/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.base.bean;

import com.yunhoo.base.db.impl.PayStyleImpl;
import com.yunhoo.base.model.MsgModel;
import com.yunhoo.base.model.PayStyle;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.ToolKit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author Administrator
 */
@ManagedBean(name ="payStyleBean")
@SessionScoped
public class PayStyleBean {

    private PayStyle pay = null;
    private List<PayStyle> payli = null;
    private DataModel<PayStyle> payModel = null;
    private boolean editAble;
    private boolean update;

    public PayStyleBean() {
        pay = new PayStyle();
        payli = PayStyleImpl.getPayStyleList();
        payModel = new ListDataModel(payli);
    }

    public void initboolean() {
        editAble = false;
        update = false;
    }

    public void getPayStyle() {
        PayStyle temp = (PayStyle) payModel.getRowData();
        pay = PayStyleImpl.getPayStylebyCode(temp.getCode().trim());
        initboolean();
    }

    public void add() {
        pay = new PayStyle();
        editAble = true;
        update = false;
    }

    public void update() {
        editAble = true;
        update = true;
    }

    public void delete() {
        PayStyleImpl.deletePayStyle(pay.getCode().trim());
        payli = PayStyleImpl.getPayStyleList();
        payModel.setWrappedData(payli);
        initboolean();
    }
    public void checkDelete(){
        MsgModel msg = PayStyleImpl.selectPayStyleMsg(pay.getCode().trim());
        System.out.println("msg:" + msg.getMsg());
        System.out.println("msg:" + msg.getMsg2());
        if (msg.getMsg().equals("") && msg.getMsg2().equals("")) {
            delete();
        } else {
             ToolKit.msgs_err("已在表" + msg.getMsg() + "  " + msg.getMsg2() + "中使用");
        }

    }

    public void cancel() {
        initboolean();
    }

    public void sava() {
        List<PayStyle> li = PayStyleImpl.checkPayStyleRepeat(pay);
        System.out.println("update : " + update);
        if ("".equals(pay.getCode().trim())) {
             ToolKit.msgs_err("代码不能为空!");
            System.out.println("代码不能为空!");
        } else if ("".equals(pay.getName().trim())) {
             ToolKit.msgs_err("名称不能为空!");
            System.out.println("名称不能为空!");
        } else {
            Map map = new HashMap();
            map.put("code", pay.getCode().trim());
            map.put("name", pay.getName().trim());
            map.put("type", pay.getType().trim());
            map.put("memo", pay.getMemo().trim());
            if (update == false) {
                if (li != null && li.size() > 0) {
                    ToolKit.msgs_err("代码或名称已存在!");
                    System.out.println("代码或名称已存在!");
                } else {                    
                    PayStyleImpl.insertPayStyle(map);
                    payli = PayStyleImpl.getPayStyleList();
                    payModel.setWrappedData(payli);
                }
            }
            if (update == true) {
                PayStyleImpl.updatePayStyle(map);
                payli = PayStyleImpl.getPayStyleList();
                payModel.setWrappedData(payli);
            }
        }
        initboolean();
    }

    public PayStyle getPay() {
        return pay;
    }

    public void setPay(PayStyle pay) {
        this.pay = pay;
    }

    public boolean isEditAble() {
        return editAble;
    }

    public void setEditAble(boolean editAble) {
        this.editAble = editAble;
    }

    public DataModel getPayModel() {
        return payModel;
    }

    public void setPayModel(DataModel payModel) {
        this.payModel = payModel;
    }

    public boolean isUpdate() {
        return update;
    }

    public void setUpdate(boolean update) {
        this.update = update;
    }
}
